﻿namespace dotnet_notes.Strategies
{
    /// <summary>
    /// 品牌添加策略
    /// </summary>
    public class BrandAddStrategy : IExamineStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using var tran = db.UseTran();

            var brand = mapper.Map<Sys_Brand>(examine);
            brand.CreateTime = DateTime.Now;

            int id = await db.Insertable(brand).ExecuteReturnIdentityAsync();

            examine.BusinessID = id;
            await db.Updateable(examine).ExecuteCommandAsync();

            await db.CommitTranAsync();
        }
    }

    /// <summary>
    /// 品牌修改策略
    /// </summary>
    public class BrandModifyStrategy : IExamineStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using var tran = db.UseTran();

            var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
            brand_query.UpdateTime = DateTime.Now;

            mapper.Map(examine, brand_query);

            await db.Updateable(brand_query).ExecuteCommandAsync();
            await db.Updateable(examine).ExecuteCommandAsync();

            await db.CommitTranAsync();
        }
    }

    /// <summary>
    /// 品牌删除策略
    /// </summary>
    public class BrandDeleteStrategy : IExamineStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using var tran = db.UseTran();

            var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
            await db.Updateable<Sys_Brand>().LogicalDeleteAsync(brand_query.ID);
            await db.Updateable(examine).ExecuteCommandAsync();

            await db.CommitTranAsync();
        }
    }

    /// <summary>
    /// 品牌恢复策略
    /// </summary>
    public class BrandRecoverStrategy : IExamineStrategy
    {
        public async Task Execute<T>(SqlSugarClient db, IMapper mapper, T examine) where T : ExamineBase, new()
        {
            using var tran = db.UseTran();

            var brand_query = await db.Queryable<Sys_Brand>().GetFirstAsync(it => it.ID == examine.BusinessID);
            await db.Updateable<Sys_Brand>().LogicalRecoverAsync(brand_query.ID);
            await db.Updateable(examine).ExecuteCommandAsync();

            await db.CommitTranAsync();
        }
    }
}
